<?php

namespace app\admin\model;

use think\Model;

class Management extends Model
{
    // 表名
    protected $name = 'management';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [
         'gift_text',
         'username_text',
         'phone_text',
         'name_text',
         'nb_text',
         'send_text',
         'send_list',
         'operator_text'
    ];
      /**
     * 导入人
     */
    public function getOperatorTextAttr($value, $data){
        $value = $value ? $value : $data['operator'];
        $username = TableField('Admin','username',$value,'userid');
        return $username;
    }   
    public function getGiftList()
    {
        return [ 1=>__('Gift 1'),2=>__('Gift 2')];
    }
    public function getSendTextAttr($value, $data)
    {
        $value = $value ? $value : $data['send'];
        $man = new ManagementSms;
        $success = $man->where('management_id',$data['id'])->where('send','=',1)->count();
        $total = $man->where('management_id',$data['id'])->where('send','=',2)->count();
        $text = '总次数：'.($success + $total);
        if($success > 0)
        {
            $text .= '成功：'.$success;
        }
        if($total > 0)
        {
            $text .= '失败：'.$total;
        }
        return $text;
    }
    public function getSendListAttr($value, $data)
    {
        $man = new ManagementSms;
        $list = $man->where('management_id',$data['id'])->select();
        $str = "<table width='100%' border='1' class=\"table table-striped table-bordered table-hover table-nowrap\">";
                $str .="<thead>";
                    $str .="<tr>";
                        $str .="<th style=\"text-align: center; vertical-align: middle;\" width='50%'>短信内容</th>";
                        $str .="<th style=\"text-align: center; vertical-align: middle;\" width='10%'>状态</th>";
                        $str .="<th style=\"text-align: center; vertical-align: middle;\" width='10%'>发送时间</th>";
                        $str .="<th style=\"text-align: center; vertical-align: middle;\" width='30%'>失败原因</th>";
                    $str .="</tr>";
                $str .="</thead>";
            foreach ($list as $value)
            {
                $str.="<tbody>";
                    $str.="<tr>";
                        $str.="<td style=\"text-align: center; vertical-align: middle;\">{$value['content']}</td>";
                        $str.="<td style=\"text-align: center; vertical-align: middle;\">".($value['send'] == 1?'成功':'失败')."</td>";
                        $str.="<td style=\"text-align: center; vertical-align: middle;\">".date('m-d H:i:s',$value['createtime'])."</td>";
                        $str.="<td style=\"text-align: center; vertical-align: middle;\">{$value['why']}</td>";
                    $str.="</tr>";
                $str.="</tbody>";
            }
        $str .="</table>";
        return $str;
    }
    public function getGiftTextAttr($value, $data)
    {
        $value = $value ? $value : $data['gift'];
        $list = $this->getGiftList();
        return isset($list[$value]) ? $list[$value] : '';
    }
     public function getUsernameTextAttr($value, $data){        
        $value = $value ? $value : $data['userid'];
        $list = TableField('WeixinUser','nickname',$value,'userid');
        return isset($list) ? $list : '';
    }   
     public function getPhoneTextAttr($value, $data){        
        $value = $value ? $value : $data['userid'];
        $list = TableField('WeixinUser','phone',$value,'userid');
        return isset($list) ? $list : '';
    }   
     public function getNameTextAttr($value, $data){        
        $value = $value ? $value : $data['userid'];
        $list = TableField('WeixinUser','name',$value,'userid');
        return isset($list) ? $list : '';
    }   
    public function getNbTextAttr($value, $data){        
        $value = $value ? $value : $data['userid'];
        $list = TableField('Special','code',$value,'userid');
        return isset($list) ? $list : '';
    }   
    

    







}
